package jmine.tec.persist.schema.impl.dialects;

import java.sql.Types;
import java.util.Collections;
import java.util.List;

import jmine.tec.persist.schema.api.SchemaColumn;

/**
 * Dialect para H2
 * 
 * @author lundberg
 */
public class H2Dialect extends AbstractSqlDialect {

    private static final int MAX_TINY = 3;

    private static final int MAX_SMALL = 5;

    private static final int MAX_INTEGER = 10;

    private static final int MAX_BIGINT = 21;

    /**
     * Construtor
     */
    public H2Dialect() {
        super(new org.hibernate.dialect.H2Dialect());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String renderType(int code, int length, int precision, int scale) {
        if (Types.NUMERIC == code && scale == 0) {
            if (precision < MAX_TINY) {
                return super.renderType(Types.TINYINT, length, precision, scale);
            } else if (precision < MAX_SMALL) {
                return super.renderType(Types.SMALLINT, length, precision, scale);
            } else if (precision < MAX_INTEGER) {
                return super.renderType(Types.INTEGER, length, precision, scale);
            } else if (precision < MAX_BIGINT) {
                return super.renderType(Types.BIGINT, length, precision, scale);
            } else {
                return super.renderType(code, length, precision, scale);
            }
        } else {
            return super.renderType(code, length, precision, scale);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<String> renderComment(String table, String comment) {
        return Collections.emptyList();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<String> renderComment(String table, String column, String comment) {
        return Collections.emptyList();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected List<String> renderIdentityPrimaryKey(String table, String name, SchemaColumn column) {
        return this.format("ALTER TABLE %s ALTER COLUMN %s IDENTITY", table, column.getName());
    }
}
